package modules

import (
	"strconv"
)

type CCC struct {
	ProjectId     string
	Env           string
	Name          string
	Conf          *[]map[string]string
	ActiveVersion string
	LatestVersion string
	Db            *DB
}

func (c *CCC) Init(id, env string) error {
	c.ProjectId = id
	c.Env = env
	err := c.GetActiveVersion()
	if err != nil {
		return err
	}
	err = c.GetLatestVersion()
	if err != nil {
		return err
	}
	return nil
}

func (c *CCC) GetActiveVersion() error {
	row, err := c.Db.SelectOne("select c_version from project_info where id=? and env=?", c.ProjectId, c.Env)
	if err != nil {
		c.ActiveVersion = ""
		return err
	}
	c.ActiveVersion = row["c_version"]
	return nil
}

func (c *CCC) SetActiveVersion(version string) error {
	v, _ := strconv.Atoi(version)
	_, err := c.Db.Update("update project_info set c_version=? where id=? and env=?", v, c.ProjectId, c.Env)
	if err != nil {
		return err
	}
	c.ActiveVersion = version
	return nil
}

func (c *CCC) GetLatestVersion() error {
	row, err := c.Db.SelectOne("select l_version from project_info where id=? and env=?", c.ProjectId, c.Env)
	if err != nil {
		c.LatestVersion = ""
		return err
	}
	c.LatestVersion = row["l_version"]
	return nil
}

func (c *CCC) SetLatestVersion(version string) error {
	v, _ := strconv.Atoi(version)
	_, err := c.Db.Update("update project_info set l_version=? where id=? and env=?", v, c.ProjectId, c.Env)
	if err != nil {
		return err
	}
	c.LatestVersion = version
	return nil
}

func (c *CCC) GetConf() error {
	rows, err := c.Db.SelectAll("select conf_key,conf_value from project_conf where id=? and env=? and version=?", c.ProjectId, c.Env, c.ActiveVersion)
	if err != nil {
		return err
	}
	c.Conf = rows
	return nil
}

func (c *CCC) SetKV(key, value string) error {
	//数据库中是否有对应的值
	v, err := c.GetValue(key)
	if err != nil {
		return err
	}
	//如果没有就插入
	pid, _ := strconv.Atoi(c.ProjectId)
	version, _ := strconv.Atoi(c.ActiveVersion)
	if v == "" {
		_, err = c.Db.Insert("insert into project_conf(id,env,version,conf_key,conf_value) values (?,?,?,?,?)", pid, c.Env, version, key, value)
		if err != nil {
			return err
		}
	} else if v == value { //取到的值与传入的值一样，直接返回
		return nil
	} else { //更新数据
		_, err = c.Db.Update("update project_conf set conf_value=? where id=? and env=? and version=? and conf_key=?", value, pid, c.Env, c.ActiveVersion, key)
		if err != nil {
			return err
		}
	}
	return nil
}

func (c *CCC) DelKV(key string) error {
	_, err := c.Db.Delete("delete from project_conf where id=? and env=? and version=? and conf_key=?", c.ProjectId, c.Env, c.ActiveVersion, key)
	if err != nil {
		return err
	}
	return nil
}

func (c *CCC) GetValue(key string) (string, error) {
	row, err := c.Db.SelectOne("select conf_value from project_conf where id=? and env=? and version=? and conf_key=?", c.ProjectId, c.Env, c.ActiveVersion, key)
	if err != nil {
		return "", err
	}
	return row["conf_value"], nil
}

func (c *CCC) NewVersion() error {
	old_version, _ := strconv.Atoi(c.ActiveVersion)
	version, _ := strconv.Atoi(c.LatestVersion)
	version += 1
	str_version := strconv.Itoa(version)

	c.SetActiveVersion(str_version)
	c.SetLatestVersion(str_version)

	sql := `insert into project_conf (id,env,version,conf_key,conf_value)
			select id,env,?,conf_key,conf_value from project_conf
			where id=? and env=? and version=?`
	_, err := c.Db.Insert(sql, version, c.ProjectId, c.Env, old_version)
	if err != nil {
		return err
	}
	return nil
}
